package com.aitaoxue.palx.db.course;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.AbstractDaoSession;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

public class CourseTypeDao extends AbstractDao<CourseType, Long> {

	public CourseTypeDao(DaoConfig config, AbstractDaoSession daoSession) {
		super(config, daoSession);
	}

	public CourseTypeDao(DaoConfig config) {
		super(config);
	}

	public static final String TABLENAME = "COURSETYPE";

	public static class Properties {
		public static final Property Id = new Property(0, Long.class, "id",
				true, "_id");
		public static final Property Type = new Property(1, Integer.class,
				"type", false, "TYPE");
		public static final Property UnreadCount = new Property(2,
				Integer.class, "unreadcount", false, "UNREADCOUNT");
	}

	public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
		String constraint = ifNotExists ? "IF NOT EXISTS " : "";
		db.execSQL("CREATE TABLE " + constraint + "\"COURSETYPE\" ("
				+ "\"_id\" INTEGER PRIMARY KEY ," + // 0:id
				"\"TYPE\" INTEGER NOT NULL UNIQUE," + // 1:type
				"\"UNREADCOUNT\" INTEGER NOT NULL);");
	}

	public static void dropTable(SQLiteDatabase db, boolean ifExists) {
		String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "")
				+ "\"COURSETYPE\"";
	}

	@Override
	protected void bindValues(SQLiteStatement stmt, CourseType entity) {
		stmt.clearBindings();

		if (entity.getId() != null) {
			stmt.bindLong(1, entity.getId());
		}
		stmt.bindLong(2, entity.getType());
		stmt.bindLong(3, entity.getUnreadcount());
	}

	@Override
	protected Long getKey(CourseType entity) {
		if (entity != null) {
			return entity.getId();
		} else {
			return null;
		}
	}

	@Override
	protected boolean isEntityUpdateable() {
		return true;
	}

	@Override
	protected CourseType readEntity(Cursor cursor, int offset) {
		CourseType type = new CourseType(cursor.getLong(offset + 0),
				cursor.getInt(offset + 1), cursor.getInt(offset + 2));
		return type;
	}

	@Override
	protected void readEntity(Cursor cursor, CourseType entity, int offset) {
		entity.setId(cursor.getLong(offset + 0));
		entity.setType(cursor.getInt(offset + 1));
		entity.setUnreadcount(cursor.getInt(offset + 2));
	}

	@Override
	protected Long readKey(Cursor cursor, int offset) {
		return cursor.getLong(0 + offset);
	}

	@Override
	protected Long updateKeyAfterInsert(CourseType entity, long rowId) {
		entity.setId(rowId);
		return rowId;
	}

}
